//
//  UIViewController+BMBubbleTransition.m
//  XLBubbleTransitionExample
//
//  Created by huyuchen on 2017/5/26.
//  Copyright © 2017年 . All rights reserved.
//

#import "UIViewController+BMBubbleTransition.h"
#import <objc/runtime.h>
#import "BMSearchAnimationTool.h"

static NSString *bmPushTransitionKey = @"bmPushTransitionKey";
static NSString *bmPopTransitionKey = @"bmPopTransitionKey";
static NSString *bmPresentTransitionKey = @"bmPresentTransitionKey";
static NSString *bmDismissTransitionKey = @"bmDismissTransitionKey";

@implementation UIViewController (BMBubbleTransition)

- (void)setBm_pushTransition:(BMSearchAnimationTool *)bm_pushTransition{
    if (bm_pushTransition) {
        bm_pushTransition.transitionType = BMSearchTransitionTypeShow;
        self.navigationController.delegate = self;
        objc_setAssociatedObject(self, &bmPushTransitionKey, bm_pushTransition, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

- (BMSearchAnimationTool *)bm_pushTransition{
    return objc_getAssociatedObject(self, &bmPushTransitionKey);
}

- (void)setBm_popTransition:(BMSearchAnimationTool *)bm_popTransition{
    if (bm_popTransition) {
        bm_popTransition.transitionType = BMSearchTransitionTypeHide;
        self.navigationController.delegate = self;
        objc_setAssociatedObject(self, &bmPopTransitionKey, bm_popTransition, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

- (BMSearchAnimationTool *)bm_popTransition{
    return objc_getAssociatedObject(self, &bmPopTransitionKey);
}

- (void)setBm_presentTransition:(BMSearchAnimationTool *)bm_presentTransition{
    if (bm_presentTransition) {
        bm_presentTransition.transitionType = BMSearchTransitionTypeShow;
        self.transitioningDelegate = self;
        objc_setAssociatedObject(self, &bmPresentTransitionKey, bm_presentTransition, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

- (BMSearchAnimationTool *)bm_presentTransition{
    return objc_getAssociatedObject(self, &bmPresentTransitionKey);
}

- (void)setBm_dismissTransition:(BMSearchAnimationTool *)bm_dismissTransition{
    if (bm_dismissTransition) {
        bm_dismissTransition.transitionType = BMSearchTransitionTypeHide;
        self.transitioningDelegate = self;
        objc_setAssociatedObject(self, &bmDismissTransitionKey, bm_dismissTransition, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

- (BMSearchAnimationTool *)bm_dismissTransition{
    return objc_getAssociatedObject(self, &bmDismissTransitionKey);
}

#pragma mark - Push,Pop转场动画设置
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    if (operation == UINavigationControllerOperationPush && [fromVC isEqual:self]) {
        return (id<UIViewControllerAnimatedTransitioning>)self.bm_pushTransition;
    }else if (operation == UINavigationControllerOperationPop && [fromVC isEqual:self]) {
        return (id<UIViewControllerAnimatedTransitioning>)self.bm_popTransition;
    }else{
        return nil;
    }
}

#pragma mark - Present,Dismiss转场动画设置
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    return (id<UIViewControllerAnimatedTransitioning>)self.bm_presentTransition;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    return (id<UIViewControllerAnimatedTransitioning>)self.bm_dismissTransition;
}
@end
